<?php
namespace app\yunda\admin;

use app\common\builder\ZBuilder;
use app\yunda\model\Active as ActiveModel;
use think\Db;

/**
 * 我的活动
 * @package app\yunda\admin
 */
class Active extends Base{
    public function index()
    {
        // 获取查询和排序规则
        $map = $this->getMap();
        $order = $this->getOrder();
        $data = Db::table('dp_yd_active',true)
            // ->view('xiaoan_post',['title'],'xiaoan_post.id=xiaoan_comment.post_id')
            ->where($map)
            ->order($order)
            ->order('id desc')
            ->paginate()->each(function($item){
                $sum=Db::table('dp_yd_active')->sum('chance');
               // $item['kai_lv']=(int)floor(($item['chance']/$sum)*100).'%';
                $item['kai_lv']=(($item['chance']/$sum)*100).'%';
                return $item;
            });
        //halt($data);
        return ZBuilder::make('table')
            ->setTableName('yd_active')  //需要修改 这里修改正确可以直接修改字段 删除
            ->addColumns([
                ['id','id'],
                ['icon','奖品图片','picture'],
                ['active_title','奖品描述'],
                ['chance','中奖权重'],
                ['kai_lv','中奖概率'],
                ['stock','库存'],
//                ['sort','排序'],
//                ['status','是否显示','switch'],
                ['create_time','创建时间','datetime'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('add', [],[])
            ->addRightButton('edit',[],[])
            ->addTopButtons(['delete'])
            ->setRowList($data)
            //->addFilter(['title' => 'admin_action', 'username'])
            ->setSearch(['id' => 'ID', 'active_title' => '奖品描述'])
            ->fetch();
    }
    public function add(){
        if($this->request->isPost()){
            // 第一步 获取数据 如果后台面向大众 需要用only方法
            $data = request()->post();
            // 第二部 验证数据
            $result = $this->validate($data,'Active');
            if($result!==true) $this->error($result);
            // 第三部 存储
            ActiveModel::create($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['image','icon','商品图片','必须'],
                ['text','active_title','奖品描述','必须'],
                ['number','chance','中奖权重','必须,数字越大概率越大'],
                ['number','stock','库存','必须'],
//                ['radio','status','是否显示','',['否','是'],1],
//                ['number','sort','排序','','',0],
            ])
            ->fetch();
    }
    public function edit($id=null){
        if($this->request->isPost()){
            $data = request()->post();
            $result = $this->validate($data,'Active');
            if($result!==true) $this->error($result);
            ActiveModel::update($data);
            $this->success('保存成功', null, '_parent_reload');
        }
        $data = ActiveModel::get($id);
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden','id'],  // 多增加一个影藏ID 即可
                ['image','icon','商品图片','必须'],
                ['text','active_title','奖品描述','必须'],
                ['number','chance','中奖权重','必须,数字越大概率越大'],
                ['number','stock','库存','必须'],
//                ['radio','status','是否显示','',['否','是']],
//                ['number','sort','排序',''],
            ])
            ->setFormData($data)
            ->fetch();
    }
}